import pandas as pd
import numpy as np
#Lib库
#————————————————————————————————————————————————————————————————————————————————————
# Series  : pandas.Series(data,index,dtype,copy)
###Series——创建Pandas系列
# name= np.array(['中国','美国','日本'])
# s= pd.Series(name)
# print(s)
# print(s.values)

###Series——访问Pandas系列
# s=pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
# print(s[1])
# print(s[:3])
# print(s[-3:])
#
# print(s['b'])
# print(s[['a','b','c']])



#————————————————————————————————————————————————————————————————————————————————————————
#DataFrame :DataFrame (data, index, columns, dtype, copy)
####从列表创建 DataFrame
## （1）单个列表创建
# data =[10,20,30,40,50]
# df =pd.DataFrame(data)
# print('单个列表创建:',df)
#
# #(2)多维列表创建
# data =[['a','10'],['b','30'],['c','50']]
# df  =pd.DataFrame(data,columns =['Name','Age'])
# print('多维列表创建 :\n ',df)
#
# #(3)从键值为ndarray/List 的字典创建 DataFrame
# data ={'Name':['Tom','Jack','Steve','Ricky'],'Age':[12,34,23,27]}
# df =pd.DataFrame(data)
# print('键值配对 :\n',df)
#
#
# #(4)从Series 字典创建 DataFrame
# d ={'one':pd.Series([1,2,3,4],index=['A','B','C','D']),
#     'two':pd.Series([5,6,7],index=['A','B','C'])}
# df = pd.DataFrame(d)
# print('字典创建:\n',df)



#——————————————————————————————————————
####DataFrame 基本功能
##（1）axes 返回行轴标签和列轴标签列表
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Row axis labels and column axis labels are:")
# print (df.axes)


##（2）dtypes 返回每列的数据类型
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("The data types of each column are:")
# print (df.dtypes)



##（3）empty 返回布尔值，表示对象是否为空，返回True 表示对象为空
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Is the object empty?")
# print (df.empty)


##（4）ndim 返回对象的维数
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our object is:")
# print (df)
# print ("The dimension of the object is:")
# print (df.ndim)





##（5）shape 返回DataFrame 的维度的元组
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our object is:")
# print (df)
# print ("The shape of the object is:")
# print (df.shape)



##（6)size 返回DataFrame 中元素的个数
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our object is:")
# print (df)
# print ("The total number of elements in our object is:")
# print (df.size)


##（7）values  将DataFrame 实际数据作为 NDarray返回
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our object is:")
# print (df)
# print ("The actual data in our data frame is:")
# print (df.values)

##（8） head() 返回前n 行 +  tail() 返回后n 行
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our data frame is:")
# print (df)
# print ("The first two rows of the data frame is:")
# print (df.head(2))
# print ("The last two rows of the data frame is:")
# print (df.tail(2))


##（9） T 转置，行列交换
# #Create a Dictionary of series
# d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
#    'Age':pd.Series([25,26,25,23,30,29,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#
# #Create a DataFrame
# df = pd.DataFrame(d)
# print ("Our data series is:")
# print (df)








#——————————————————————————————————————————————————————
####DataFrame 的行列操作
##(1)选择列
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
#
# df = pd.DataFrame(d)
# print (df ['one'])

##（2）添加列 + 列相加
# d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#      'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
# df = pd.DataFrame(d)
# print("Adding a new column by passing as Series:")
# df['three'] = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# print(df)
#
# print("Adding a new column using the existing columns in DataFrame:")
# df['four'] = df['one'] + df['three']
# print(df)




##（3)删除列
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
#    'three' : pd.Series([10,20,30], index=['a','b','c'])}
#
# df = pd.DataFrame(d)
# print ("Our dataframe is:")
# print (df)
#
# # using del function
# print ("Deleting the first column using DEL function:")
# del df['one']
# print (df)
#
# # using pop function
# print ("Deleting another column using POP function:")
# df.pop('two')
# print (df)


#(4)选择行
#####按标签选:将行标签传递给一个loc 函数
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
#
# df = pd.DataFrame(d)
# print (df.loc['b'])#b代表前两行


#####根据整数位置进行选择: 将整数位置传递给 iloc 选择行
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
#
# df = pd.DataFrame(d)
# print (df.iloc[2])#2代表索引值

#####使用' :' 选择多行
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
#
# df = pd.DataFrame(d)
# print (df[2:4])


# ##（5）添加行
# df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
# df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
#
# df = df._append(df2)
# print ('添加行 :\n',df)
#
# ##（6)删除行
# df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
# df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
#
# df = df._append(df2)
#
# # Drop rows with label 0
# df = df.drop(0)

# print ('删除行 :\n',df)














#——————————————————————————————————————————————————————————————————————————————————————————